<?php 

class Model_CreditScore extends Model {

    protected $_table_name = 'cmr_credit_score';
    protected $_primary_key = 'id';

    //配置信息
    public function getConfig(){
        return array(
            'works_score' => 20,
            'basic_score' => 20,
            'deposit_score' => 30,
            'public_score' => 5,
        );
    }

    //更新用户信用分
    public function updateCredit($user_id,$field){

        $c = $this->getConfig();

        if(!in_array($field,array_keys($c))){
            return;
        }

        $info = $this->db->query('SELECT * FROM ' . $this->_table_name . ' where user_id = '.$user_id)->current();

        if(empty($info)){
            $data = array(
                'user_id' => $user_id,
                'total_score' => 0,
                'works_score' => 0,
                'basic_score' => 0,
                'deposit_score' => 0,
                'public_score' => 0,
            );
            $this->db->insert($this->_table_name, $data);

            $info = $this->db->query('SELECT * FROM ' . $this->_table_name . ' where user_id = '.$user_id)->current(); //重新获取数据
        }

        if(isset($info[$field])){
            if($info[$field] > 0 && $field == 'deposit_score'){ //字段值大于0 即为减少信用分
                $this->db->query('UPDATE ' . $this->_table_name . " SET $field = 0 ,total_score = total_score - $c[$field] where id = ".$info['id'] );
            }else{ //否则增加信用分
                $this->db->query('UPDATE ' . $this->_table_name . " SET $field = $c[$field] ,total_score = total_score + $c[$field] where id = ".$info['id'] );
            }
        }


    }
    
}
	